package com.zhangxu.basemvp.presenter;

import android.text.TextUtils;

import com.zhangxu.basemvp.bean.UserBean;
import com.zhangxu.basemvp.base.BasePresenter;
import com.zhangxu.basemvp.contract.LoginContract;
import com.zhangxu.basemvp.model.LoginModelImpl;

import java.util.HashMap;

import javax.inject.Inject;

/**
 * Time:2019/4/10
 * Author:zhangxu
 */
public class LoginPresenterImpl extends BasePresenter<LoginContract.View>
        implements LoginContract.Presenter {
    private LoginContract.Model model;
    LoginContract.View loginView;

    @Inject
    public LoginPresenterImpl(LoginContract.View loginView) {
        this.loginView = loginView;
        model = new LoginModelImpl();
    }

    public void login() {
        loginView.showLoading();
        String name = loginView.getUserName();
        String pwd = loginView.getPassWord();
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
            loginView.showMessage("请输入用户名或密码");
            return;
        }
        HashMap map = new HashMap();
        map.put("userName", name);
        map.put("password", pwd);
        model.login(map, this);

    }

    @Override
    public void onSuccess(UserBean user) {
        loginView.loginSuccess(user);
        loginView.dismissLoading();

    }

    @Override
    public void onfail(String msg) {
        loginView.showMessage(msg);
        loginView.dismissLoading();
    }
}
